"""
	Visual Source Safe Integration Module.
"""

class sourcesafe:
	"""
	Simple vss wrapper, to make lifetime management easier.
	"""
	
	VSSFLAG_REPASK			= 0x40		# local copy(writable): ask
	VSSFLAG_RECURSNO        = 0x1000	# recursive: no
	VSSFLAG_RECURSYES		= 0x2000	# recursive: yes
	VSSFLAG_FORCEDIRNO		= 0x4000	# override working folder specifications

	def __init__(self, iniFile, username, password = None):
		import win32com.client

		self.iniFile		= iniFile
		self.username		= username
		self.password		= password

		self.__vss			= win32com.client.Dispatch('SourceSafe')
		self.__vss.Open(self.iniFile, self.username, self.password)

	def __validateProjectPath(self, projectpath):
		if not isinstance(projectpath, str):
			raise TypeError('Invalid projectpath type, expect str')
		
		if not projectpath.startswith('$/'):
			projectpath		= '$/{}'.format(projectpath)

		return projectpath

	def get(self, projectpath, localpath):
		'''
		Get file or folder files from source safe server
		'''
		
		projectpath	= self.__validateProjectPath(projectpath)

		vssItem		= self.__vss.VSSItem(projectpath, False)
		vssItem.Get(localpath, self.VSSFLAG_REPASK | self.VSSFLAG_RECURSYES | self.VSSFLAG_FORCEDIRNO)

###########################################################################

if __name__ == "__main__":
	config		= r"\\elementserver\REW_DevLatest_ElementClientVSS\srcsafe.ini"
	username	= "pw_prg_lixianmin"
	password	= None

	srcsafe	= sourcesafe(config, username, password)
	srcsafe.get('REWReportBugs', 'c:\\safe')

